<?php 
class url
{
	var $db;
	var $URLRULE;
	var $SETTING;
	var $CATEGORY;

	function url()
	{
		global $db, $CATEGORY, $MODEL, $URLRULE, $SETTING;
		$this->db = &$db;
		//$this->c = is_object($c) ? $c : load('content.class.php','content');
		$this->URLRULE = $URLRULE;
		$this->SETTING = $SETTING;
		$this->CATEGORY = $CATEGORY;
		$this->MODEL = $MODEL;
	}

	function index()
	{
		return 'index.'.$this->SETTING['fileext'];
	}

	function category($catid, $page = 0, $isurls = 0, $type = 3)
	{
		global $SETTING,$CATEGORY;
		
		//if(!isset($this->CATEGORY[$catid])) return false;
		
        $C = cache_read('category_'.$catid.'.php');
		
		// 当是外部链接的时候直接返回
		if($C['type'] == 2)
		{
			return $C['url']; exit();
		}
		
		if(is_numeric($page)) $page = intval($page);
		$categorydir = $C['parentdir'].$C['catdir'];
		$catdir = $C['catdir'];
		
		
		// 栏目是否静态与动态在网站配置里设置（影响所有栏目）
		if($SETTING['ishtml']){
			
			//$url = $CATEGORY[$catid]['parentdir'].$CATEGORY[$catid]['catdir'];
			
			//$url = $page ? "category/".$CATEGORY[$catid]['catid'].','.$page.'.'.$SETTING['fileext'] : "category/".$CATEGORY[$catid]['catid'].'.'.$SETTING['fileext'];
			
			$url = $page ? "{$categorydir}/{$page}.{$SETTING['fileext']}" : "{$categorydir}/index.{$SETTING['fileext']}";
			
			// 更新栏目静态页
			//$html = load('html.class.php','html');
			//$html->category($catid);
		}
		else{
			//$url = "list.php?catid=$catid";
			
			$url = $page ? url("?module=category&action=view&catid=$catid&page=$page") : url("?module=category&action=view&catid=$catid");
			//$url = "./?list/{$catid}.{$SETTING['fileext']}";
		}
		return $url;
	}
	
	// 新增单网页
	function page($pageid, $page = 0, $isurls = 0, $type = 3)
	{
		global $SETTING,$CATEGORY;
		
		//if(!isset($this->CATEGORY[$catid])) return false;
		
        $C = cache_read('page_'.$pageid.'.php');
		

		if(is_numeric($page)) $page = intval($page);
		//$categorydir = $C['parentdir'].$C['catdir'];
		//$catdir = $C['catdir'];
		
		
		// 栏目是否静态与动态在网站配置里设置（影响所有栏目）
		if($SETTING['ishtml']){
			
			//$url = $CATEGORY[$catid]['parentdir'].$CATEGORY[$catid]['catdir'];
			
			//$url = $page ? "category/".$CATEGORY[$catid]['catid'].','.$page.'.'.$SETTING['fileext'] : "category/".$CATEGORY[$catid]['catid'].'.'.$SETTING['fileext'];
			
			$url = $page ? "{$categorydir}/{$page}.{$SETTING['fileext']}" : "{$categorydir}/index.{$SETTING['fileext']}";
			
			// 更新栏目静态页
			//$html = load('html.class.php','html');
			//$html->category($catid);
		}
		else{
			//$url = "list.php?catid=$catid";
			
			$url = $page ? url("?module=page&pageid=$pageid&page=$page") : url("?module=page&pageid=$pageid");
			//$url = "./?list/{$catid}.{$SETTING['fileext']}";
		}
		return $url;
	}
	
	function show($contentid, $page = 0, $catid = 0, $time = 0, $prefix = '')
	{
		global $SETTING;
		if($SETTING['ishtml']){
				
			$contentid = $prefix ? $prefix : $contentid;
			$url = $page ? "show/{$contentid},{$page}.{$SETTING['fileext']}" : "show/{$contentid}.{$SETTING['fileext']}";
			
		}
		else{
			// 内容页的动态地址
			//$url = $page ? "./?show/{$contentid},{$page}.{$SETTING['fileext']}" : "./?show/{$contentid}.{$SETTING['fileext']}";
			$url = $page ? url("?module=content&action=view&contentid=$contentid&page=$page") : url("?module=content&action=view&contentid=$contentid");
		}
		return $url;
	}

	function show_pages($page, $pagenumber, $pageurls)
	{
		$pages = '';
		for($i=1; $i<=$pagenumber; $i++)
		{
			$pages .= $page == $i ? '<span>'.$i.'</span>' : '<a href="'.$pageurls[$i][1].'">'.$i.'</a>';        
		}
		$prepage = max($page-1, 1);
		$nextpage = min($page+1, $pagenumber);
		return '<a href="'.$pageurls[$prepage][1].'">上一页</a>'.$pages.'<a href="'.$pageurls[$nextpage][1].'">下一页</a>';
	}

	function update($contentid,$url)
	{
		if(!$this->db->get_one("SELECT contentid FROM `".DB_PRE."content` WHERE `contentid`='$contentid' AND `url`='$url'"))
		{
			return $this->db->query("UPDATE `".DB_PRE."content` SET url='$url' WHERE `contentid`='$contentid'");
		}
		return true;
	}
}
?>